package com.example.demo.service.impl;

import com.example.demo.entity.Personnelmanagespecificpeople;
import com.example.demo.handle.SpecificPersonnelListParseHandler;
import com.example.demo.mapper.PersonnelmanagespecificpeopleMapper;
import com.example.demo.service.PersonnelmanagespecificpeopleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-22
 */
@Service
public class PersonnelmanagespecificpeopleServiceImpl extends ServiceImpl<PersonnelmanagespecificpeopleMapper, Personnelmanagespecificpeople> implements PersonnelmanagespecificpeopleService {

    @Autowired
    private PersonnelmanagespecificpeopleMapper personnelmanagespecificpeopleMapper;

    @Override
    public Integer insertSpecificPeopleData(String personnelXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            SpecificPersonnelListParseHandler specificPersonnelListParseHandler = new SpecificPersonnelListParseHandler();
            try {
                // 读取xml文件
                parser.parse(personnelXml, specificPersonnelListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + specificPersonnelListParseHandler.getPersonnelmanageareaArrayList().size() + "个节点被存储");
            for (Personnelmanagespecificpeople personnelmanagespecificpeople : specificPersonnelListParseHandler.getPersonnelmanageareaArrayList()) {
                System.out.println(personnelmanagespecificpeople.getCsMineCode());
                System.out.println(personnelmanagespecificpeople.getCsDataTime());
                System.out.println(personnelmanagespecificpeople.getPsPersonCard());
                System.out.println(personnelmanagespecificpeople.getPsShouldPlace());
                System.out.println(personnelmanagespecificpeople.getPsShouldTime());
                System.out.println(personnelmanagespecificpeople.getPsGisnodeCode());
                System.out.println("----finish----");
                personnelmanagespecificpeopleMapper.insertSpecificPeopleData(personnelmanagespecificpeople.getCsMineCode(),personnelmanagespecificpeople.getCsDataTime(),personnelmanagespecificpeople.getPsPersonCard(),personnelmanagespecificpeople.getPsShouldPlace(),personnelmanagespecificpeople.getPsShouldTime(),personnelmanagespecificpeople.getPsGisnodeCode());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
